﻿using DataEntity.Entities;
using IService;
using Microsoft.EntityFrameworkCore;
using Model.AccountDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;

namespace Service
{
    public class AccountService : IAccountService
    {
        #region 构造函数依赖注入

        private readonly DbContext _dbContext;
        public AccountService(DbContext dbContext)
        {
            _dbContext = dbContext;
        }

        

        #endregion
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="regInput"></param>
        /// <returns></returns>
        public bool register(AccountInput regInput)
        {
            var Iuser = _dbContext.Set<Userinfo>().Where(u => u.UserName == regInput.UserName).FirstOrDefault();
            if (Iuser!=null)
            {
                return false;
            }
            else
            {
                _dbContext.Add(new Userinfo
                {
                    UserName=regInput.UserName,

                    UserPwd= MD5Helper.EncryptString(regInput.UserPwd),
                    UserEmail=regInput.UserEmail,
                    UserEnable="1",
                    UserMember="0",
                    IsOwner=false,
                });
                int results = _dbContext.SaveChanges();
                if (results > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //登录
        public bool Login(AccountOutput userOutput)
        {
            var results=_dbContext.Set<Userinfo>().Where(u => u.UserName == userOutput.UserName &&u.UserPwd== MD5Helper.EncryptString(userOutput.UserPwd)).FirstOrDefault();
            if (results!=null)
            {
                if (results.UserEnable== "1")
                {
                    return true;
                }
                return false;
            }
            else
            {
                return false;
            }
        }
    }
}
